<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="200px"  label-position="right">
    <el-form-item label="课程名称" prop="courseName">
      <el-input v-model="ruleForm.courseName" style="width: 300px"></el-input>
    </el-form-item>
    <el-form-item label="讲师名称" prop="teacherName">
      <el-input v-model="ruleForm.teacherName" style="width: 300px"></el-input>
    </el-form-item>
    <el-form-item label="课程简介" prop="courseIntro">
      <el-input v-model="ruleForm.courseIntro" style="width: 400px"></el-input>
    </el-form-item>
    <el-form-item label="课程类型" prop="courseStyle">
      <el-radio v-model="ruleForm.courseStyle" label="1" border @change="isChangeShow">线上视频自学</el-radio>
      <el-radio v-model="ruleForm.courseStyle" label="2" border @change="isChangeShow">线下实训学习</el-radio>
    </el-form-item>
    <el-form-item label="考核类型" prop="examineType">
      <el-radio v-model="ruleForm.examineType" label="3" border @change="isChangeShow2">线上系统考核</el-radio>
      <el-radio v-model="ruleForm.examineType" label="4" border @change="isChangeShow2">线下试卷考核</el-radio>
    </el-form-item>
    <el-form-item label="课程方向" prop="courseDir" >
      <el-select v-model="ruleForm.courseDir" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <!--根据上面的单选框选择出现的组件-->
    <el-form-item label="传入线上学习视频" v-if="isShowAddVideo">
      <el-upload
        style="width: 400px"
        auto-upload="false"
        action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="3"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传视频压缩文件，上传顺序默认为视频集数</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="输入线下学习地点和时间" v-if="isShowAddPlace" prop="studyPlace">
      <span>请输入课程培训时间描述</span>
      <el-input v-model="ruleForm.studyPlace" style="width: 300px"></el-input>
      <span>请输入课程地点</span>
      <el-input v-model="ruleForm.studyPlace" style="width: 300px"></el-input>
    </el-form-item>

    <el-form-item label="传入线上考试文件" v-if="isShowAddtest">
      <el-upload
        style="width: 400px"
        auto-upload="false"
        action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="3"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传EXCLE文件</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="输入线下考试地点和地点" v-if="isShowAddtestPlace" prop="testPlace">
      <span>请输入课程考试时间描述</span>
      <el-input v-model="ruleForm.testPlace" style="width: 300px"></el-input>
      <span>请输入课程考试地点</span>
      <el-input v-model="ruleForm.testPlace" style="width: 300px"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
    import {validateChinese} from "../../tools/validate";

    export default {
        name: "AddTrainCourse",
      //生成未选择的数据
      data() {
        //穿梭框的数据项
        const generateData = _ => {
          const data = [];
          for (let i = 1; i <= 15; i++) {
            data.push({
              key: i,
              label: `课程- ${ i }`,
            });
          }
          return data;
        };


        return {
          //上传文件的控件数据
          fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
          //控制课程类型子组件的显示
          isShowAddVideo:false,
          isShowAddPlace:false,
          isShowAddtest:false,
          isShowAddtestPlace:false,
          //表单的各个数据项的值
          ruleForm: {
            courseName:'',
            teacherName:'',
            courseIntro:'',
            courseStyle:'',
            examineType:'',
            courseDir:'',
            studyPlace:'',
            testPlace:''

          },
          //课程方向选择框数据项
          options: [{
            value: '选项1',
            label: '大数据方向'
          }, {
            value: '选项2',
            label: '人事管理方向'
          }, {
            value: '选项3',
            label: '前端方向'
          }, {
            value: '选项4',
            label: 'Java开发方向'
          }, {
            value: '选项5',
            label: '数据库维护方向'
          }],
          //穿梭框存放未选择的数据
          data: generateData(),
          //穿梭框存放选择课程的数据
          value:[],
          rules: {
            courseName: [
              { required: true, message: '请输入课程名称', trigger: 'blur' },
              { min: 0, max: 15, message: '长度在 0 到 15 个字符', trigger: 'blur' },
            ],
            teacherName: [
              { required: true, message: '请输入讲师名称', trigger: 'blur' },
              { min: 0, max: 8, message: '长度在 0 到 8 个字符', trigger: 'blur' },
              {validator:validateChinese,trigger: 'blur'}

            ],
            courseIntro:[
              { required: true, message: '请输入课程简介', trigger: 'blur' },
            ],
            courseStyle:[
              { required: true, message: '请选择至少一种类型', trigger: 'blur' },
            ],
            examineType:[
              { required: true, message: '请选择至少一种类型', trigger: 'blur' },
            ],
            courseDir:[
              { required: true, message: '请选择至少一种方向', trigger: 'blur' },
            ],
            studyPlace:[
              { required: true, message: '请填写上课地点', trigger: 'blur' },
            ],
            testPlace:[
              { required: true, message: '请填写考试地点', trigger: 'blur' },
            ],
          },

        };
      },
      methods: {
          //上传文件的控件方法
        handleRemove(file, fileList) {
          console.log(file, fileList);
        },
        handlePreview(file) {
          console.log(file);
        },
        handleExceed(files, fileList) {
          this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        beforeRemove(file, fileList) {
          return this.$confirm(`确定移除 ${ file.name }？`);
        },
          //控制课程类型子组件是否显示
        isChangeShow(value){
          if(value=="1"){
            this.isShowAddVideo=true;
              this.isShowAddPlace=false;

          }
          if(value=="2"){
            this.isShowAddVideo=false;
            this.isShowAddPlace=true;
          }

        },
        isChangeShow2(value){
          if(value=="3"){
            this.isShowAddtest=true;
            this.isShowAddtestPlace=false;
          }
          if(value=="4"){
            this.isShowAddtest=false;
            this.isShowAddtestPlace=true;
          }
        },
        //表单提交
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              alert('submit!');
            } else {
              console.log('error submit!!');
              return false;
            }
          });
          this.flush();
        },
        //重置表单数据
        resetForm(formName) {
          this.$refs[formName].resetFields();
          this.isShowAddVideo=false;
          this.isShowAddPlace=false;
          this.isShowAddtest=false;
          this.isShowAddtestPlace=false;
        },



      }
    }
</script>

<style scoped>

</style>
